﻿using System;
using System.Windows;
using System.Windows.Controls;
using LiveTiss.LiveTissWCF;

namespace LiveTiss.Telas
{
    public partial class LoginScreen : UserControl
    {
        private AuthenticationToken token = null;
        private LiveTissServiceClient client;

        public LoginScreen()
        {
            InitializeComponent();

            txtError.Visibility = System.Windows.Visibility.Collapsed;

            txtUsuario.Focus();

            client = new LiveTissServiceClient();

            client.ValidateTokenCompleted += new EventHandler<ValidateTokenCompletedEventArgs>(client_ValidateTokenCompleted);
            client.LoginGetByTokenCompleted += new EventHandler<LoginGetByTokenCompletedEventArgs>(client_LoginGetByTokenCompleted);
        }

        private void txtUsuario_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                if (string.IsNullOrEmpty(txtUsuario.Text))
                {
                    txtUsuario.Focus();
                }
                else
                {
                    txtSenha.Focus();
                }
            }
        }

        private void txtSenha_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                if (string.IsNullOrEmpty(txtSenha.Password))
                {
                    txtSenha.Focus();
                }
                else
                {
                    this.btnLogin_Click(btnLogin, new RoutedEventArgs());
                }
            }
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsuario.Text) || string.IsNullOrEmpty(txtSenha.Password))
            {
                txtError.Visibility = System.Windows.Visibility.Visible;
                return;
            }

            txtError.Visibility = System.Windows.Visibility.Collapsed;

            txtUsuario.IsEnabled = false;
            txtSenha.IsEnabled = false;
            btnLogin.IsEnabled = false;
            token = Classes.Util.GenerateToken(txtUsuario.Text, txtSenha.Password);

            client.ValidateTokenAsync(token);
        }

        void client_ValidateTokenCompleted(object sender, ValidateTokenCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                txtUsuario.IsEnabled = true;
                txtSenha.IsEnabled = true;
                btnLogin.IsEnabled = true;

                // exception
            }
            else if (e.Result)
            {
                Classes.Util.Token = token;

                client.LoginGetByTokenAsync(token);
            }
            else
            {
                txtUsuario.IsEnabled = true;
                txtSenha.IsEnabled = true;
                btnLogin.IsEnabled = true;

                txtError.Visibility = System.Windows.Visibility.Visible;

                txtSenha.Password = string.Empty;
                txtSenha.Focus();
            }
        }

        void client_LoginGetByTokenCompleted(object sender, LoginGetByTokenCompletedEventArgs e)
        {
            txtUsuario.IsEnabled = true;
            txtSenha.IsEnabled = true;
            btnLogin.IsEnabled = true;

            if (e.Error != null)
            {
                // exception
            }
            else
            {
                Classes.Util.Login = e.Result;

                // ir para a proxima tela
                App.SwitchControl(new MenuScreen());
            }
        }
    }
}
